var patientId = null;

$(document).ready(function() {
	$(".dateformat").each(function() {
		var date = new Date($(this).html());
		$(this).html((dateFormat(date, "ddd, m/d/yy, h:MM TT")));
	});
	$(".dateonlyformat").each(function() {
		var date = new Date($(this).html());
		$(this).html((dateFormat(date, "dddd, mmmm d, yyyy")));
	});

	patientId = $("#patientId").html();
});

function openConsultationRecord(recordId) {
	$.post("ConsultationRecord", {
		request : "open-record",
		recordId : recordId,
	}, function(data) {
		openConsultationRecordResponse(data);
	});
}

function openConsultationRecordResponse(data) {
	var complaints = data.complaints;
	var diagnosis = data.diagnosis;
	var prescription = data.prescription;
	var staff = data.staff;
	var price = data.price;
	var date = dateFormat(data.date, "dddd, mmmm d, yyyy");

	$("#view_consultation_record_prescription").html(prescription);
	$("#view_consultation_record_price").html(price);
	$("#view_consultation_record_staff").html(staff);
	$("#view_consultation_record_date").html(date);
	$("#view_consultation_record_complaints").html(complaints);
	$("#view_consultation_record_diagnosis").html(diagnosis);

	$("#view_consultation_record_modal").modal("show");
}

function clearConsultationForm() {
	$("#complaints_input").val("");
	$("#diagnosis_input").val("");
	$("#prescription_input").val("");
	$("#price_input").val("");
}

function addConsultationRecord() {
	var complaints = $("#complaints_input").val();
	var diagnosis = $("#diagnosis_input").val();
	var prescription = $("#prescription_input").val();
	var price = $("#price_input").val();

	$.post("ConsultationRecord", {
		request : "add-record",
		patientId : patientId,
		complaints : complaints,
		diagnosis : diagnosis,
		prescription : prescription,
		price : price
	}, function(data) {
		addConsultationRecordResponse(data);
	});
}

function addConsultationRecordResponse(data) {
	$("#consultation_records_table tbody").append(
			buildConsultationRecordTableRow(data.recordId, data.complaints,
					data.staff));

	$('#new_consultation_record_modal').modal('hide');
}

function buildConsultationRecordTableRow(recordId, complaints, staff) {
	return '<tr>'
			+ '<td>'
			+ recordId
			+ '</td>'
			+ '<td><p style="height: 20px; width: 60ex; overflow: hidden; padding: 0px; margin: 0px; cursor: pointer;"'
			+ '		onclick="openConsultationRecord(' + recordId + ')">' + '<a>'
			+ complaints + '</a>' + '	</p></td>' + '<td>' + staff + '</td>'
			+ '</tr>';
}

var first = true;
var labTestIdList = new Array();

function openDiagnosticTestRecord(id) {
	$.post("DiagnosticTestRecord", {
		mode : "open-record",
		labTestRecordId : id
	}, function(data) {
		openDiagnosticTestRecordResponse(data);
	});
}

function openDiagnosticTestRecordResponse(data) {
	var status = data.status;
	var totalPrice = data.totalPrice;
	var staff = data.staff;
	var date = dateFormat(data.date, "dddd, mmmm d, yyyy");
	var labTests = data.labTests;

	$("#view_diagnostic_test_record_id").html(data.id);
	$("#view_diagnostic_test_record_staff").html(staff);
	$("#view_diagnostic_test_record_price").html(totalPrice);
	$("#view_diagnostic_test_record_date").html(date);
	$("#view_diagnostic_test_record_status").html(status);

	for ( var i = 0; i < labTests.length; i++) {
		var test = labTests[i];
		$("#view_diagnostic_test_record_tests tbody").append(
				buildViewDiagnosticTestRecordTestsTableRow(test.name,
						test.category, test.price));
	}

	$("#view_diagnostic_test_record_modal").modal('show');
}

function buildViewDiagnosticTestRecordTestsTableRow(name, category, price) {
	return '<tr><td>' + category + '</td><td>' + name + '</td><td>' + price
			+ '</td></tr>';
}

function clearDiagnosticTestForm() {
	$("input[name=diagnosticTestStatus]:checked").prop('checked', false);
	console.log($("#list_of_tests_table tbody").children().size());
	$("#list_of_tests_table tbody").children().remove();
	$("#list_of_tests_table tbody").append(
			'<tr><td colspan="2">Add Diagnostic Tests</td></tr>');
	first = true;
	labTestIdList = new Array();
}

function addDiagnosticTestRecord() {
	var status = $("input[name=diagnosticTestStatus]:checked").val();

	var list = JSON.stringify(labTestIdList);

	$.post("DiagnosticTestRecord", {
		mode : "add-diagnostic-test-record",
		status : status,
		patientId : patientId,
		labTestIdList : list
	}, function(data) {
		addDiagnosticTestRecordResponse(data);
	});
}

function addDiagnosticTestRecordResponse(data) {
	var id = data.id;
	var date = data.date;
	var staff = data.staff;

	console.log(date);

	$("#diagnostic_tests_table").append(
			buildDiagnoticTestRecordRow(id, date, staff));

	$('#new_diagnostic_test_record_modal').modal('hide');
}

function buildDiagnoticTestRecordRow(id, date, staff) {
	return '<tr>'
			+ '<td>'
			+ id
			+ '</td>'
			+ '<td><p style="height: 20px; width: 60ex; overflow: hidden; padding: 0px; margin: 0px; cursor: pointer;"'
			+ 'onclick="openDiagnosticTestRecord(' + id + ')">' + '<a>'
			+ dateFormat(date, "dddd, mmmm d, yyyy") + ' Test' + '</a>'
			+ '</p></td>' + '<td>' + staff + '</td>' + '</tr>';
}

function addTest() {
	if (first) {
		$("#list_of_tests_table tbody").children().remove();
		first = false;
	}

	var cost = parseFloat("" + $("select#labTests").val());
	var testName = $("select#labTests option:selected").text();
	var labTestId = $("select#labTests option:selected").attr("labTestId");

	$("#list_of_tests_table tbody").append(
			'<tr><td>' + labTestId + ' - ' + testName + '</td><td>' + cost
					+ '</td></tr>');

	labTestIdList.push(labTestId);

	console.log(labTestIdList);
}

function clearDentalForm() {
	console.log("dental form");
}

function addDentalRecord() {
	console.log("add dental record");

}